<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/12/15
 * Time: 17:35
 */

$key=$_COOKIE['userkey'];
session_start();
if(!isset($_SESSION[$key])){
    echo '<a href="../login/loginUI.php">请重新登陆</a>';
    die;
}
$CardNo=trim($_POST['CardNo']);
$this_cardno=$_SESSION[$key]['user_name'];

if($CardNo==$this_cardno){
    echo '删除的卡号不能跟本卡一致<a href="delete_card.php">返回</a>';
    die();
}
$conn=new  mysqli('127.0.0.1','root','root','bank');
//查询本账户的卡id
$sql="select * from bankcard WHERE CardNo='$this_cardno';";
$result=$conn->query($sql);
$data=$result->fetch_assoc();
$cardin_id=$data['CardId'];
//查询要消除账户的余额
$sql="select * from bankcard WHERE CardNo='$CardNo';";
$result=$conn->query($sql);
$data=$result->fetch_assoc();
$money=$data['CardMoney'];
$card_id=$data['CardId'];
//转出
$sql="UPDATE bankcard SET CardMoney=CardMoney-CardMoney WHERE CardNo='$CardNo';";
$conn->query($sql);
//添加交易记录
$sql="INSERT INTO cardexchange(CardId,MoneyInBank,MoneyOutBank,ExchangeTime) 
VALUES('$card_id','$money','0',NOW());";
$conn->query($sql);
//转入
$sql="UPDATE bankcard SET CardMoney=CardMoney+'$money' WHERE CardNo='$this_cardno';";
$result=$conn->query($sql);
//添加交易记录
$sql="INSERT INTO cardexchange(CardId,MoneyInBank,MoneyOutBank,ExchangeTime) 
VALUES('$cardin_id','0','$money',NOW());";
$conn->query($sql);
//添加转账记录
$sql="INSERT INTO cardtransfer(CardidOut,CardIdin,TransferMoney,TransferTime) VALUES
('$card_id','$cardin_id','$money',NOW());";
echo $sql;
$conn->query($sql);
echo $conn->affected_rows;
//删除
$sql="UPDATE bankcard SET CardState=-1 WHERE CardNo='$CardNo';";
$result=$conn->query($sql);
header('location:../loginAction.php');



